package com.easylive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.easylive.entity.constants.Constants;
import com.easylive.entity.enums.ResponseCodeEnum;
import com.easylive.entity.enums.UserActionTypeEnum;
import com.easylive.entity.enums.VideoTopTypeEnum;
import com.easylive.entity.po.UserInfo;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.query.UserInfoQuery;
import com.easylive.entity.query.VideoInfoQuery;
import com.easylive.exception.BusinessException;
import com.easylive.mappers.UserInfoMapper;
import com.easylive.mappers.VideoInfoMapper;
import org.springframework.stereotype.Service;

import com.easylive.entity.enums.PageSize;
import com.easylive.entity.query.VideoCommentQuery;
import com.easylive.entity.po.VideoComment;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.query.SimplePage;
import com.easylive.mappers.VideoCommentMapper;
import com.easylive.service.VideoCommentService;
import com.easylive.utils.StringTools;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


/**
 * 评论 业务接口实现
 */
@Service("videoCommentService")
public class VideoCommentServiceImpl implements VideoCommentService {

	@Resource
	private VideoCommentMapper<VideoComment, VideoCommentQuery> videoCommentMapper;

	@Resource
	private VideoInfoMapper<VideoInfo, VideoInfoQuery> videoInfoMapper;

	@Resource
	private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<VideoComment> findListByParam(VideoCommentQuery param) {
		if (param.getLoadChildren() != null && param.getLoadChildren()) {
			// 加载子评论
			return this.videoCommentMapper.selectListWithChildren(param);
		}
		return this.videoCommentMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(VideoCommentQuery param) {
		return this.videoCommentMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<VideoComment> findListByPage(VideoCommentQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<VideoComment> list = this.findListByParam(param);
		PaginationResultVO<VideoComment> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(VideoComment bean) {
		return this.videoCommentMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<VideoComment> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.videoCommentMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<VideoComment> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.videoCommentMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(VideoComment bean, VideoCommentQuery param) {
		StringTools.checkParam(param);
		return this.videoCommentMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(VideoCommentQuery param) {
		StringTools.checkParam(param);
		return this.videoCommentMapper.deleteByParam(param);
	}

	/**
	 * 根据CommentId获取对象
	 */
	@Override
	public VideoComment getVideoCommentByCommentId(Integer commentId) {
		return this.videoCommentMapper.selectByCommentId(commentId);
	}

	/**
	 * 根据CommentId修改
	 */
	@Override
	public Integer updateVideoCommentByCommentId(VideoComment bean, Integer commentId) {
		return this.videoCommentMapper.updateByCommentId(bean, commentId);
	}

	/**
	 * 根据CommentId删除
	 */
	@Override
	public Integer deleteVideoCommentByCommentId(Integer commentId) {
		return this.videoCommentMapper.deleteByCommentId(commentId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void postComment(VideoComment videoComment, Integer replyCommentId) {
		/**
		 * 添加记录，视频更新评论数
		 */
		VideoInfo videoInfo = videoInfoMapper.selectByVideoId(videoComment.getVideoId());
		// 先判断这个视频是否关闭评论
		if (ObjectUtils.isEmpty(videoInfo)) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		if (!StringTools.isEmpty(videoInfo.getInteraction()) && videoInfo.getInteraction().contains("1")) {
			throw new BusinessException("该视频已关闭评论");
		}
		// 如果是回复的评论
		if (replyCommentId != null) {
			// 先判断回复的评论是否存在
			VideoComment replyComment = videoCommentMapper.selectByCommentId(replyCommentId);
			if (ObjectUtils.isEmpty(replyComment) || !replyComment.getVideoId().equals(videoComment.getVideoId())) {
				throw new BusinessException(ResponseCodeEnum.CODE_600);
			}
			// 如果当前回复的评论是一级评论
			if (replyComment.getpCommentId() == 0) {
				videoComment.setpCommentId(replyComment.getCommentId());
			} else {
				videoComment.setpCommentId(replyComment.getpCommentId());
				videoComment.setReplyUserId(replyComment.getUserId());
			}
			UserInfo replyUserInfo = userInfoMapper.selectByUserId(replyComment.getUserId());
			videoComment.setReplyNickName(replyUserInfo.getNickName());
			videoComment.setReplyAvatar(replyUserInfo.getAvatar());
		} else {
			videoComment.setpCommentId(Constants.ZERO);
		}
		videoComment.setPostTime(new Date());
		videoComment.setVideoUserId(videoInfo.getUserId());
		videoCommentMapper.insert(videoComment);

		// 更新视频的评论数，只算一级评论
		if (videoComment.getpCommentId() == 0) {
			videoInfoMapper.updateCountInfo(videoInfo.getVideoId(), UserActionTypeEnum.VIDEO_COMMENT.getField(), Constants.ONE);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateTopComment(VideoComment videoComment) {
		VideoCommentQuery query = new VideoCommentQuery();
		query.setTopType(VideoTopTypeEnum.TOP.getCode());
		query.setVideoId(videoComment.getVideoId());
		VideoComment updateComment = new VideoComment();
		updateComment.setTopType(VideoTopTypeEnum.NO_TOP.getCode());
		videoCommentMapper.updateByParam(updateComment, query);

		// 再更新本评论的状态
		updateComment.setTopType(VideoTopTypeEnum.TOP.getCode());
		videoCommentMapper.updateByCommentId(updateComment, videoComment.getCommentId());
	}

	@Override
	public void deleteVideoCommentByCommentIdOrPcommentId(Integer commentId) {
		videoCommentMapper.deleteByCommentIdOrPcommentId(commentId);
	}
}